if not cache then 
    print("DisplayLib :: table cache is not defined") 
end

            --[[ ########## Timer Hijack ########## ]--

This override is here to snag every timer and cache it into
an array for later cancelling. There is no change in syntax,
none of your code will break.

Sometimes you will want to cancel all timers EXCEPT a few,
in which case simply pass in "false" when you create your timer.

]]

local timerArray = {}

cache.performWithDelay = timer.performWithDelay
timer.performWithDelay = function( time, callback, repeats, add )
    local repeats, add = repeats, add
    if type(repeats) == "boolean" then 
        add = repeats; 
        repeats = nil 
    end
    
    local t = cache.performWithDelay(time, callback, repeats)
    t._begin = system.getTimer()
    t._delay = t._delay or time
    
    t.cancel = function()
        timer.cancel(t)
        local v = table.search(timerArray, t)
        if v then 
            table.remove(timerArray, v) 
        end
        t, v = nil, nil
    end
    
    t.pause = function()
        t._remaining = (t._count or 1 * t._delay) - (system.getTimer() - t._begin)
        timer.cancel(t)
        t.paused = true
    end
    
    t.resume = function()
        if t.paused then
            local tmp = t._delay
            if repeats then
            else
                t = cache.performWithDelay(t._remaining, callback, add)
            end
            t._delay = tmp
            t._begin = system.getTimer()
            t.paused, t._remaining, tmp = nil, nil, nil
        end
    end
    
    if add == true then
        timerArray[#timerArray+1] = t
    end
    
    return t
end

timer.cancelAll = function()
    while #timerArray > 0 do
        timer.cancel(table.remove(timerArray,1))
    end
end

            --[[ ########## Safe Timer Cancel ########## ]--

There will inevitably come a time when you write something like:

    if myTimer then timer.cancel(myTimer) end

Instead of wasting time thinking about if the timer exists yet,
just cancel it like normal. If is doesn't exist, you will receive
a kind warning letting you know, but there be no error

]]

cache.timerCancel = timer.cancel
timer.cancel = function(t) 
    if t and t._time then 
        cache.timerCancel(t) 
    end 
end
